package com.ali.thread;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class Product {
    private Integer num;

    public synchronized boolean sell1() {
        return sell();
    }

    private boolean sell() {
        String name = Thread.currentThread().getName();
        log.info("线程名：" + name);
        log.info("当前产品数：" + num);

        if (num > 0) {
            num = num - 1;
            log.info(name + "售卖一个产品");
            log.info("剩余产品数：" + num);
            return true;
        } else {
            return false;
        }
    }

    public boolean sell2() {
        synchronized (num) {
            return sell();
        }
    }

    public boolean sell3() {
        synchronized (this) {
            return sell();
        }
    }
}
